package com.springmvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.handler.Handler;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    //在目标方法执行之前 执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle 执行 ...");

        String para = request.getParameter("para");

        if ("yes".equals(para)) {
            return true;
        } else {
            request.getRequestDispatcher("/req.jsp").forward(request, response);
            return false;
        }

    }

    @Override
    //在目标方法执行之后 视图对象返回之前执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle 执行 ...");

        //对视图参数进行干预
        modelAndView.addObject("name","李冰洋");
    }

    @Override
    //在流程都执行完毕后 执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion 执行 ...");
    }
}
